package com.cici;

import com.baomidou.mybatisplus.mapper.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;

import java.util.Date;

/**
 * 公共字段填充
 */
@Configuration
public class CustomMetaObjectHandler extends MetaObjectHandler {

    private static final Logger LOGGER = LoggerFactory.getLogger(CustomMetaObjectHandler.class);


    @Override
    public void insertFill(MetaObject metaObject) {
        Object createDate = getFieldValByName("createDate", metaObject);
        Object version = getFieldValByName("version", metaObject);
        Object updateDate = getFieldValByName("updateDate", metaObject);
        Object delFlag = getFieldValByName("delFlag", metaObject);

        if (createDate == null) {
            setFieldValByName("createDate", new Date(), metaObject);
        }
        if (updateDate == null) {
            setFieldValByName("updateDate", new Date(), metaObject);
        }
        if (version == null) {
            setFieldValByName("version", 1, metaObject);
        }
        if (delFlag == null) {
            setFieldValByName("delFlag", 1, metaObject);
        }

    }

    @Override
    public void updateFill(MetaObject metaObject) {
        setFieldValByName("updateDate", new Date(), metaObject);

    }
}
